from PyQt5.QtCore import QDir, QFileInfo, QStandardPaths, Qt, QUrl,QSettings
import os
import json
import myglobal
import sys
import logging
_setting_file="setting.json"
setting={
}
path='HKEY_CURRENT_USER\\Software\\NCS_REPORT'
setting2 = QSettings(path, QSettings.NativeFormat)
init_value=""
def write_setting():
    logging.info("write_setting=====================")
    new_value=json.dumps(setting)
    if new_value==init_value:
        return
    logging.info("output")
    setting_filename = os.path.join(myglobal.initpath, _setting_file)
    set_file=open(setting_filename, 'w',encoding="utf-8")
    set_file.write(new_value)
    set_file.close()
    if sys.platform == "linux":
        pass
    else:#write to reg
        setting2.setValue("yqbh",setting["yqbh"])
        setting2.setValue("sblx",setting["sblx"])
        setting2.setValue("output_folder",setting["output_folder"])
        setting2.setValue("database",setting["database"])
def read_setting():
    global setting,init_value
    bookmark_file_name = os.path.join(QDir.toNativeSeparators(_config_dir()),
                                  _setting_file)
    file=open(bookmark_file_name,encoding="utf-8")
    setting=json.load(file)
    file.close()
    if sys.platform == "linux":
        pass
    else:#read from reg
        setting["yqbh"]=setting2.value("yqbh")
        setting["sblx"]=setting2.value("sblx")
        setting["output_folder"]=setting2.value("output_folder")
        setting["database"]=setting2.value("database")
    setting["ffbh"].sort()
    setting["syy"].sort()
    init_value=json.dumps(setting)
def _config_dir():
    location =myglobal.initpath#QDir.currentPath()#QStandardPaths.writableLocation(QStandardPaths.ConfigLocation)
    logging.info("configdir"+location)
    return '%s' % location
def init_from_xls():
    global setting
    from ffbh import getData
    read_setting()
    data=getData()
    ff=data[0]
    syy=data[1]
    ysry=data[2]
    yqbh=data[3]
    logging.info(ff,syy,ysry,yqbh)
    setting["ffbh"]=[]
    setting["syy"]=[]
    setting["ysry"]=[]
    for i in ff:
        setting["ffbh"].append(i)
    for i in syy:
        setting["syy"].append(i)
    for i in ysry:
        setting["ysry"].append(i)
    write_setting()
if __name__ == '__main__':
    init_from_xls()
